﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using System.Xml;
using System.ServiceModel.Syndication;
using DTOSW.Core.Membership.Functions;

namespace DTOWS.Controllers
{
    [HandleError]
    public class NewsController : Controller
    {

        public ActionResult News()
        {
            ViewData["Message"] = "News!";
            ViewData["BodyID"] = "NewsSite";
            ViewData["BodyClass"] = "StuffSite";
            string myValue = (string)Session["UserName"];

            if(User.Identity.IsAuthenticated)
            {    
                return View();
            }
            return RedirectToAction("Index", "Debat");
        }


        private string rss;
        private string rssTitle;
        private List<string> newsTitle;
        private List<string> newsSummary;
        private List<string> newsLink;
        private List<int> newsID;
        private List<string> newsDate;
        private List<string> newsAuther;
        public ActionResult GetFeed(string url)
        {


            newsTitle = new List<string>();
            newsSummary = new List<string>();
            newsLink = new List<string>();
            newsID = new List<int>();
            newsDate = new List<string>();
            newsAuther = new List<string>();

            int antalNews = 0;

            using (XmlReader reader = XmlReader.Create(url)) {

                SyndicationFeed rssData = SyndicationFeed.Load(reader);
                rssTitle = rssData.Title.Text;


                int i = 0;
                foreach (SyndicationItem c in rssData.Items) {
                    i++;
                    DateTime dateTime = c.PublishDate.DateTime;
                    string date = dateTime.ToShortDateString() + " - " + dateTime.ToShortTimeString();

                    newsTitle.Add(c.Title.Text);
                    newsSummary.Add(c.Summary.Text);
                    newsID.Add(i);
                    newsDate.Add(date);
                    newsLink.Add(c.Id);



                }

                antalNews = i;
            }



            object newsObj = new {
                Newstitle = newsTitle,
                NewsSummary = newsSummary,
                NewsID = newsID,
                NewsDate = newsDate,
                AntalNews = antalNews,
                NewsLink = newsLink,
                RssTitle = rssTitle
            };

            return Json(newsObj);

        }



    }
}
